#include "ParticleForceRegistry.h"
#include <algorithm>


namespace SimPhy
{


ParticleForceRegistry::ParticleForceRegistry()
{
}

void ParticleForceRegistry::add(Particle *particle, AbstractParticleForceGenerator *forceGenerator)
{
    ParticleForceRegistration registration;
    registration.m_particle=particle;
    registration.m_forceGenerator=forceGenerator;

    m_registrations.push_back(registration);
}

bool operator==(ParticleForceRegistry::ParticleForceRegistration const& r1, ParticleForceRegistry::ParticleForceRegistration const& r2)
{
        if (r1.m_particle==r2.m_particle && r1.m_forceGenerator==r2.m_forceGenerator)
            return true;
        else
            return false;
}


void ParticleForceRegistry::remove(Particle* particle, AbstractParticleForceGenerator *forceGenerator)
{
    ParticleForceRegistration registration;

    registration.m_particle=particle;
    registration.m_forceGenerator=forceGenerator;

    std::vector<ParticleForceRegistration>::iterator it;
    it = std::find(m_registrations.begin(), m_registrations.end(), registration);

    if(it !=m_registrations.end())
        m_registrations.erase(it);
}

void ParticleForceRegistry::clear()
{
    m_registrations.clear(); // Delete only the pointors. It doesn't call the destructors.
}

void ParticleForceRegistry::updateForces(real timeStep)
{
    for(std::vector<ParticleForceRegistration>::iterator it=m_registrations.begin(); it!=m_registrations.end(); it++ )
        it->m_forceGenerator->updateForce(it->m_particle, timeStep);
}



}
